﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;

namespace SharpChannel.Util
{
    public class ConfigurationUtil
    {

        public static Hashtable GetSharpChannelConfiguration()
        {
            return ConfigurationManager.GetSection("sharp-channel") as Hashtable;
        }

        internal static int GetReceiveBufferedQueueSize()
        {
            return GetIntValue("buffer.queue.size", 1024 * 1024);
        }

        public static int GetIntValue(string key, int defaultValue = 0)
        {
            int value = 10000;
            var dic = GetSharpChannelConfiguration();
            if (dic != null && dic.ContainsKey(key))
                value = int.Parse(dic[key].ToString());
            return value;

        }

        public static string GetStringValue(string key, string defaultValue)
        {
            var dic = GetSharpChannelConfiguration();
            return (dic != null && dic.ContainsKey(key)) ? dic[key].ToString() : defaultValue;
        }

        public static int GetSessionInitGlobalSequenceId()
        {
            return GetIntValue("session.init.sequence", 10000);
        }


        public static int GetReceiveBufferSize()
        {
            return GetIntValue("receive.buffer.size", 1024 * 16);
        }

        public static int GetListenPort()
        {
            return GetIntValue("listen.port", 10086);
        }

        public static int GetConnectPort()
        {
            return GetIntValue("connect.port", 10086);
        }

        public static int GetRecvTimeoutValue()
        {
            return GetIntValue("receive.timeout", 10);
        }

        public static string GetConnectIP()
        {
            return GetStringValue("connect.ip", "127.0.0.1");
        }
    }
}
